package org.sgroup.imenu.util;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.text.DecimalFormat;

import org.sgroup.imenu.service.XmlDocument;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.ConnectivityManager;
import android.util.Log;

public class CData {
	Context context;

	public CData(Context context) {
		this.context = context;
	}

	public CData() {
	}

	// This function returns true if Internet connected otherwise returns false
	public Boolean CheckConnection() {
		boolean connected= false;
		try{
		ConnectivityManager conMgr = (ConnectivityManager) this.context
				.getSystemService(Context.CONNECTIVITY_SERVICE);
		connected = (conMgr.getActiveNetworkInfo() != null
				&& conMgr.getActiveNetworkInfo().isAvailable() && conMgr
				.getActiveNetworkInfo().isConnected());
		}catch(Exception e){
			e.printStackTrace();
		}
		return connected;
	}
    public Boolean CheckVersionData(String dataFile, String vesrion){
	    String data = ReadInnerStorage(dataFile);
	    if (data =="") return false;
		Document doc = XmlDocument.LoadXml(data);
		NodeList nodelist = (NodeList) XmlDocument.selectNodes(doc, "/Data");// doc.getChildNodes();
		Element e = (Element)nodelist.item(0);
		String val = e.getAttribute("version");
		if( val.equalsIgnoreCase(vesrion))
			return true;
    	return false;
    	
    }
	private String ReadConfigAsset(String dataFile) {
		try {

			InputStream is = context.getAssets().open(dataFile);
			// We guarantee that the available method returns the total
			// size of the asset... of course, this does mean that a single
			// asset can't be more than 2 gigs.
			int size = is.available();
			// Read the entire asset into a local byte buffer.
			byte[] buffer = new byte[size];
			is.read(buffer);
			is.close();
			// Convert the buffer into a string.
			String text = new String(buffer);
			return text;
		} catch (IOException e) {
			return "";
		}
	}

	public Boolean SaveInnerStorage(String data, String dataFile) {
		Boolean result = true;
		try {
			FileOutputStream fos = this.context.openFileOutput(dataFile,
					Context.MODE_PRIVATE);
			fos.write(data.getBytes());

			fos.close();
		} catch (Exception e) {
			result = false;
		}
		return result;

	}

	public void saveToInternalSorage(Bitmap bitmapImage, String filename,
			String Path) {
		// ContextWrapper cw = new ContextWrapper(this.context);
		// ContextWrapper cw = new
		// ContextWrapper(this.contex.getApplicationContext());
		String ext = filename.substring(filename.lastIndexOf(".") + 1);

		if (bitmapImage == null)
			return;

		File directory = context.getFilesDir();
		directory.mkdir();
		int index = Path.lastIndexOf("/");
		if (index == -1) {
			directory = new File(directory, Path);
			directory.mkdir();
		} else {
			String[] array = Path.split("/");
			for (int t = 0; t < array.length; t++) {
				directory = new File(directory, array[t]);
				
				directory.mkdir();
			}
		}

		/*
		 * File directory = this.context.getDir(Path, Context.MODE_PRIVATE); if
		 * (!directory.exists()) { try { directory.createNewFile(); } catch
		 * (IOException e) { // TODO Auto-generated catch block
		 * e.printStackTrace(); } directory.mkdir(); }
		 */
		File mypath = new File(directory, filename);
		FileOutputStream fos = null;
		try {
			// fos = openFileOutput(filename, Context.MODE_PRIVATE);
			//System.out.println("mypath = " + mypath);
			fos = new FileOutputStream(mypath);

			// Use the compress method on the BitMap object to write image to
			// the OutputStream
			bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
	}

	private File[] loadInternalImages(String Path) {
		File directory = this.context.getDir(Path, Context.MODE_PRIVATE);
		File[] imageList = directory.listFiles();
		if (imageList == null) {
			imageList = new File[0];
		}
		Log.i("My", "ImageList Size = " + imageList.length);
		return imageList;
	}

	public String ReadInnerStorage(String dataFile) {
		BufferedReader bufferedReader = null;
		try {
			bufferedReader = new BufferedReader(new FileReader(new File(
					this.context.getFilesDir() + File.separator + dataFile)));
		} catch (FileNotFoundException e) {
			
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		String read;
		StringBuilder builder = new StringBuilder("");

		try {
			while ((read = bufferedReader.readLine()) != null) {
				builder.append(read);
			}
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		try {
			bufferedReader.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return "";
		}
		return builder.toString();

	}
	
	public static String FormatPrice(double price)
	{		
		DecimalFormat formatter = new DecimalFormat("#,###");
		return formatter.format(price) + " \u0111";
	}
}
